#!/usr/bin/python
#
# Copyright (C) 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__author__ = 'ales.kotnik@gmail.com (Ales Kotnik)'


from xmlhelper import XmlHelper

class Invoice:
  def recalculate(self):
    self.total = 0
    for item in self.lineitems:
      self.total += float(item.qty) * float(item.item_price)
    # 20% ddv TODO: v parameter
    self.ddv = self.total * 0.2
    self.total_ddv = self.total + self.ddv
    
  def toXml(self):
    self.recalculate()
    xml = "<Invoice>\n"
    xml += XmlHelper.addTag("Id",self.id)
    xml += XmlHelper.addTag("Total",self.total)
    xml += XmlHelper.addTag("Ddv",self.ddv)
    xml += XmlHelper.addTag("TotalDdv",self.total_ddv)
    
    xml += XmlHelper.addTag("DocumentDate",self.document_date)
    xml += XmlHelper.addTag("DateDue",self.date_due)
    xml += XmlHelper.addTag("DateFrom",self.date_from)
    xml += XmlHelper.addTag("DateTo",self.date_to)        
    
    xml += "<InvoicingParty>"+ self.me.toXml() + "</InvoicingParty>\n"
    xml += "<InvoicedParty>" + self.party.toXml() + "</InvoicedParty>" 
    
    xml += "<LineItems>"
    for item in self.lineitems:
      xml += item.toXml()
    xml += "</LineItems>"
    xml += XmlHelper.addTag('id', self.id)
    xml += "</Invoice>\n"
    return xml

class InvoiceLineItem:
  def toXml(self):  
    xml  ="<LineItem>"
    xml += XmlHelper.addTag("Id",self.id)
    xml += XmlHelper.addTag("AssetId",self.asset_id)
    xml += XmlHelper.addTag("AssetDesc",self.asset_desc)
    xml += XmlHelper.addTag("ItemPrice",self.item_price)
    xml += XmlHelper.addTag("Qty",self.qty)
    xml += XmlHelper.addTag("Uom",self.uom)
    xml += XmlHelper.addTag("Total",float(self.qty)*float(self.item_price))
    xml +="</LineItem>"
    return xml

